package com.jfy.cmai.robot.http;

import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

import okhttp3.logging.HttpLoggingInterceptor;
import okio.Buffer;
import okio.BufferedSource;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {

    public static final String API_HOST = "http://192.168.3.192:8003";//http://172.16.1.66:8080/jyl/
    public static final String IMAGE_URL = "https://jingfang.net/image/";
    private static RetrofitClient instance;
    private static OkHttpClient okHttpClient;
    private static Retrofit retrofit;

    private RetrofitClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new HttpBaseParamsInterceptor())
                .addInterceptor(new HeaderInterceptor())
                .addInterceptor(interceptor)
                .readTimeout(15, TimeUnit.SECONDS)
                .connectTimeout(15, TimeUnit.SECONDS)
                .addInterceptor(responseInterceptor)
                .build();

        /*okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new HttpBaseParamsInterceptor())
                .build();*/

        retrofit = new Retrofit.Builder()
                .baseUrl(API_HOST)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
    }

    //增加响应体解密
    Interceptor responseInterceptor = new Interceptor() {
        @NotNull
        @Override
        public Response intercept(@NotNull Interceptor.Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);

            if(response.isSuccessful()){
                ResponseBody responseBody = response.body();
                if(responseBody != null){
                    /**开始解密*/
                    try{
                        BufferedSource source = responseBody.source();
                        source.request(Long.MAX_VALUE);
                        Buffer buffer = source.buffer();
                        Charset charset = Charset.forName("UTF-8");
                        MediaType contentType = responseBody.contentType();
                        if(contentType != null){
                            charset = contentType.charset(charset);
                        }
                        String bodyString = buffer.clone().readString(charset);
//                        String responseData = DecryptUtils.decrypt(DecryptUtils.reverse(eData).substring(2, 18), eData.substring(4, 20), bodyString);
//                        ResponseBody newResponseBody = ResponseBody.create(contentType, responseData.trim());
                        Log.i("okHttp", bodyString);
                        response = response.newBuilder().body(responseBody).build();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }

            return response;
        }
    };

    public static RetrofitClient getInstance() {
        if (instance == null) {
            synchronized (RetrofitClient.class) {
                if (instance == null) {
                    instance = new RetrofitClient();
                }
            }
        }
        return instance;
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }
}